public class Main {
    public static void main(String[] args) {
        Solution solution = new Solution();
        solution.isHappy(19);
    }
}
class Solution {
    public boolean isHappy(int n) {
        int slow = n;
        int fast = n;
         do {
            slow = split(slow);
            fast = split(fast);
            fast = split(fast);
        } while(slow==fast);
        if (fast == 1) return true;
        else return false;
    }
    private int split(int n) {
        int result = 0;
        int count = 0;
        int n1 = n;
        while (n>0) {
            count++;
            n /= 10;
        }
        for (int i = 0;i < count;i++) {
            int t = n1 % 10;
            result += t * t;
            n1 /= 10;
        }
        return result;
    }
}